package com.taxi.driver.module.main.mine.message;

import android.content.Context;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.TextView;

import com.qianxx.adapter.internal.SuperViewHolder;
import com.qianxx.view.refreshview.RefreshAdapter;
import com.taxi.driver.R;
import com.taxi.driver.module.vo.MessageVO;
import com.taxi.driver.util.TimeUtils;

import java.util.ArrayList;

public class MessageAdapter extends RefreshAdapter<MessageVO> {

    private String uuid;

    public MessageAdapter(Context context) {
        super(context, new ArrayList<>(), R.layout.item_message);
    }

    @Override
    public void onBind(SuperViewHolder holder, int viewType, int position, MessageVO item) {
        if (uuid != null && uuid.equals(item.uuid)) {
            item.status = 2;
        }

        if (item.status == 1 || item.status == 4) {
            holder.setVisibility(R.id.img_status, View.VISIBLE);
        } else {
            holder.setVisibility(R.id.img_status, View.GONE);
        }
        holder.setText(R.id.tv_type, item.type == 1 ? "系统消息" : item.title);
        holder.setBackgroundResource(R.id.img_system, item.type == 1 ? R.drawable.system_icon_notice : R.drawable.system_icon_pay);
        holder.setText(R.id.tv_time, TimeUtils.paseDateAndTime(item.sendTime));
        if (item.sendType == 1) {
            holder.setText(R.id.tv_content, item.content);
        } else {
            holder.setText(R.id.tv_content, item.title);
        }
        TextView tvContent = holder.getView(R.id.tv_content);
        setLines(tvContent);
    }

    private void setLines(final TextView tv) {
        ViewTreeObserver observer = tv.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                // TODO Auto-generated method stub
                ViewTreeObserver obs = tv.getViewTreeObserver();
                obs.removeGlobalOnLayoutListener(this);
                if (tv.getLineCount() > 2) {
                    int lineEndIndex = tv.getLayout().getLineEnd(1);
                    String text = tv.getText().toString().substring(0, lineEndIndex - 3) + "...";
                    tv.setText(text);
                }
            }
        });
    }

    void setRead(String uuid) {
        this.uuid = uuid;
    }
}
